﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class KhoList : System.Web.UI.Page
    {
        IUnitOfWork iunitfwork = new EFUnitOfWork();
        CHUCNANG objchucnang = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            KiemtraQUyenSuDung();
            if (!IsPostBack)
            {
                LoadHienthi();
            }

        }
        /// <summary>
        /// Load Hiển thị
        /// </summary>
        public void LoadHienthi()
        {
            KHORepository repository = new KHORepository(new EFRepository<KHO>(), iunitfwork);
            IQueryable<KHO> objKho = repository.All();
            Grkho.DataSource = objKho;
            Grkho.DataBind();
        }

        protected void Grkho_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbtnsua")
            {
                Response.Redirect("KhoAdd.aspx?MAKHO=" + e.CommandArgument);
            }
            if (e.CommandName == "lbtnxoa")
            {
                try
                {
                    KHORepository repository = new KHORepository(new EFRepository<KHO>(), iunitfwork);
                    KHO objKho = repository.GetById(e.CommandArgument.ToString());
                    repository.Delete(objKho);
                    repository.Save();
                    LoadHienthi();
                }
                catch (Exception ex)
                {
                    RegisterClientScriptBlock("Thông báo", "<script> alert('Không thể xóa được')</script>");
                }
            }
        }
        public void KiemtraQUyenSuDung()
        {
            lbtnthemmoi.Visible = false;
            if (Session["MANV"] != null)
            {
                string strMANV = Session["MANV"].ToString();
                IUnitOfWork iunitwork = new EFUnitOfWork();
                lbtnthemmoi.Visible = false;
                NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitwork);
                NHANVIEN objNhanvien = repository.All().Where(p => p.MANV == strMANV).FirstOrDefault();
                if (objNhanvien != null)
                {
                    objchucnang = Commomcs.QuyenTruyCapChucnang(Request.Url.AbsolutePath, objNhanvien.MANV);
                    if (objchucnang != null)
                    {
                        if (objchucnang.ISADD == true)
                        {
                            lbtnthemmoi.Visible = true;
                        }
                        else
                        {
                            lbtnthemmoi.Visible = false;
                        }

                    }
                }
            }
        }
        protected void Grkho_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            KHO objKho = ((KHO)e.Row.DataItem);
            if (objKho != null)
            {
                Label lbnhanvien = ((Label)e.Row.FindControl("lbnhanvien"));
                if (!string.IsNullOrEmpty(objKho.MANV))
                {
                    lbnhanvien.Text = objKho.NHANVIEN.TENNV;
                }
                Label lbtinhtrang = ((Label)e.Row.FindControl("lbtinhtrang"));
                if (objKho.TINHTRANG == true)
                {
                    lbtinhtrang.Text = "Hoạt động";
                }
                else
                {
                    lbtinhtrang.Text = "Không hoạt động";
                }
            }
            LinkButton lbtnsua = ((LinkButton)e.Row.FindControl("lbtnsua"));
            LinkButton lbtnxoa = ((LinkButton)e.Row.FindControl("lbtnxoa"));
            lbtnsua.Visible = false;
            lbtnxoa.Visible = false;
            if (objchucnang != null)
            {
                lbtnsua.Visible = false;
                if (lbtnsua != null)
                {
                    if (objchucnang.ISEDIT == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnsua.Visible = false;
                    }
                }
                lbtnxoa.Visible = false;
                if (lbtnxoa != null)
                {
                    if (objchucnang.ISDELETE == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnxoa.Visible = false;
                    }
                }
            }
        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("KhoAdd.aspx");
        }

        protected void Grkho_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            LoadHienthi();
            Grkho.PageIndex = e.NewSelectedIndex;
            Grkho.DataBind();
            
        }

    }
}